package com.mxprojects.language
{
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	public class Language
	{
		private static var mainXML:XML;
		private static var extraXML:XML;
		private static var setLanguage:String;
		
		public function Language()
		{
			trace('This is a static class! Do NOT use "new Language()" instead use "Language.load(Word.(your word here));');
		}
		
		public static function main(word:String, isCaps:Boolean = false, forceAllLowerCase:Boolean = false):String {
			//search the <language>.xml for the right words
			return searchXML(mainXML, word, isCaps, forceAllLowerCase);
		}
		
		public static function extra(word:String, isCaps:Boolean = false, forceAllLowerCase:Boolean = false):String {
			//this will hunt the extra.xml and return the value
			return searchXML(extraXML, word, isCaps, forceAllLowerCase);
		}
		
		public static function setupLanguage(language:String, isExtra:Boolean = false):void {
			//use this to start the whole shabaz
			var randomNumber:Number = Math.random()*234232342;
			
			//setup the loader
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurity);
			
			//define the language
			setLanguage = language;
			
			var xmlFile:String = '/main.xml';
			if(isExtra){
				//load the socal network language
				xmlFile = '/extra.xml';
				xmlLoader.addEventListener(Event.COMPLETE, handleExtra);
			}else{
				xmlLoader.addEventListener(Event.COMPLETE, handleComplete);
			}
			
			var xmlLocation:URLRequest = new URLRequest(Config.INSTALL_DIRECTORY + Config.LANGUAGE_DIRECTORY + language + xmlFile);
			xmlLocation.data = randomNumber;
			xmlLocation.method = URLRequestMethod.GET;
			
			//load the file
			xmlLoader.load(xmlLocation);
		}
		
		private static function searchXML(xmlFile:XML, word:String, isCaps:Boolean, forceAllLowerCase:Boolean):String {
			var str:String = xmlFile..*[word].text();
			
			if(isCaps){
				str = str.toLocaleUpperCase();
			}
			
			if(forceAllLowerCase){
				str = str.toLocaleLowerCase();
			}
			
			return str;
		}
		
		
		private static function handleComplete(event:Event):void {
			//put the xml data in the holder
			trace(event);
			mainXML = new XML(event.target.data);
			
			//load the extra XML data
			setupLanguage(setLanguage, true);			
		}
		
		private static function handleExtra(event:Event):void {
			//parse the XML
			extraXML = new XML(event.target.data);
			
			//load the style sheet to load the rest of the forum
			Styles.setup(Config.INSTALL_DIRECTORY + Config.CSS_LOCATION);
		}
		
		private static function handleError(event:IOErrorEvent):void {
			//do stuff for errors here
		}
		
		private static function handleSecurity(event:SecurityErrorEvent):void {
			//display something about the error
			DefaultDialog.show("FATAL ERROR: Language did not load");
		}

	}
}